/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controle.modelo;

import controle.database.ConectaBanco;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import modelo.Cliente;

/**
 *
 * @author Luiz Cunha
 */
public class ControleCliente
{

    ConectaBanco conn;

    public ControleCliente()
    {
        conn = new ConectaBanco();
    }

    public List<Cliente> getClienteNomeLike(String nomeLike)
    {
        conn.conexao();
        try
        {
            List<Cliente> tempListCliente = new ArrayList<>();
            if (nomeLike.isEmpty())
            {
                nomeLike = "";
            }
            String query = "SELECT * FROM cliente WHERE nome LIKE '%X%'";
            query = query.replace("X", nomeLike);
            PreparedStatement pst = conn.conn.prepareStatement(query);
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next())
            {
                tempListCliente.add(preencheCliente(resultSet));
            }

            conn.desconecta();
            return tempListCliente;

        }
        catch (Exception e)
        {
            e.printStackTrace();
            conn.desconecta();
        }
        return null;
    }

    public Cliente getClienteByNome(String nome)
    {
        conn.conexao();
        try
        {
            PreparedStatement pst = conn.conn.prepareStatement("select * from cliente where nome = ?");
            pst.setString(1, nome);
            ResultSet resultSet = pst.executeQuery();
            resultSet.next();
            Cliente clienteDeRetorno = preencheCliente(resultSet);
            conn.desconecta();
            return clienteDeRetorno;
        }
        catch (Exception e)
        {
            conn.desconecta();

        }
        return null;
    }

    public Cliente getClienteById(Integer id)
    {
        conn.conexao();
        try
        {
            PreparedStatement pst = conn.conn.prepareStatement("select * from cliente where id_cliente = ?");
            pst.setInt(1, id);
            ResultSet resultSet = pst.executeQuery();
            resultSet.next();
            Cliente clienteDeRetorno = preencheCliente(resultSet);
            conn.desconecta();
            return clienteDeRetorno;
        }
        catch (Exception e)
        {
            conn.desconecta();

        }
        return null;
    }

    private Cliente preencheCliente(ResultSet resultSet) throws SQLException
    {
        Cliente tempCliente = new Cliente(resultSet.getInt("id_cliente"), resultSet.getString("nome"), resultSet.getString("endereco"), resultSet.getString("telefone"), resultSet.getString("email"));
        return tempCliente;
    }

    public List<Cliente> getAllClientes()
    {
        List<Cliente> tempListCliente = new ArrayList<>();
        conn.conexao();
        try
        {
            PreparedStatement pst = conn.conn.prepareStatement("select * from cliente");
            ResultSet resultSet = pst.executeQuery();
            while (resultSet.next())
            {
                tempListCliente.add(preencheCliente(resultSet));
            }
            conn.desconecta();
            return tempListCliente;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            conn.desconecta();
        }
        return null;

    }

    public void addCliente(Cliente cliente)
    {
        conn.conexao();
        try
        {
            PreparedStatement pst = conn.conn.prepareStatement("insert into cliente (nome, endereco, telefone, email) values (?,?,?,?)");
            pst.setString(1, cliente.getNome());
            pst.setString(2, cliente.getEndereco());
            pst.setString(3, cliente.getTelefone());
            pst.setString(4, cliente.getEmail());
            pst.execute();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            conn.desconecta();
        }
        conn.desconecta();
    }

    public void deleteCliente(Cliente cliente)
    {
        conn.conexao();
        try
        {
            PreparedStatement pst = conn.conn.prepareStatement("delete from cliente where id_cliente = ?");
            pst.setInt(1, cliente.getIdCliente());
            pst.execute();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            conn.desconecta();
        }
    }

    public void updateCliente(Cliente cliente)
    {
        conn.conexao();
        try
        {
            PreparedStatement pst = conn.conn.prepareStatement("UPDATE cliente SET nome = ?, endereco = ?, telefone = ?, email = ? where id_cliente = ?");
            pst.setString(1, cliente.getNome());
            pst.setString(2, cliente.getEndereco());
            pst.setString(3, cliente.getTelefone());
            pst.setString(4, cliente.getEmail());
            pst.setInt(5, cliente.getIdCliente());
            pst.execute();
            conn.desconecta();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            conn.desconecta();
        }

    }
}
